package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
@Autowired
private SetMealService setMealService;

    /**
     * 新增套餐信息以及对应的菜品信息
     * @param setmealDto
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
public void save(@RequestBody SetmealDto setmealDto){
    setMealService.saveSetMealAndDish(setmealDto);
}


    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        IPage<SetmealDto> p = setMealService.getPage(page, pageSize, name);
        return R.success(p);
    }

    /**
     * 批量删除或者删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
public R deleteSetMealAndDish(@RequestParam List<Long> ids){
        setMealService.deleteSetMealAndDish(ids);

        return R.success();
}

    /**
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
public  R list(Setmeal setmeal){
        //返回整个列表,就是list集合
List<Setmeal> setmealList=setMealService.getSetMealListByCid(setmeal);

        return R.success(setmealList);
}

}
/*
* 设置SpringCache:
*   导入坐标
*   application.yml配置
*   开始注解支持
*   再指定的方法上配置缓存
*
* */